<template>
  <div class="joinHome">
    <div class="joinHome_box">
      <h1 class="hello">加入房间</h1>
      <div class="joinHome_form1">
        <h3>房号：</h3>
        <el-input placeholder="（必填）" v-model.trim="room" clearable></el-input>
      </div>
      <div calss="joinHome_form2">
        <h3>昵称：</h3>
        <el-input placeholder="（必填）" v-model.trim="name" clearable></el-input>
      </div>
      <div class="joinHome_btn">
        <el-button type="info" plain @click="firm" class="one">确认加入</el-button>
        <router-link to="/">
          <el-button type="info" plain>返回主页</el-button>
        </router-link>
      </div>
    </div>
  </div>
</template>

<script>
import ADDRESS from '../assets/backendApi'

import { SET_USERNAME, SET_ROOMID } from '../store/events'

export default {
  data() {
    let name = window.localStorage.getItem('username')
    if (!name) name = ''
    let room = window.localStorage.getItem('roomid')
    if (!room) room = ''
    return {
      room,
      name
    }
  },
  methods: {
    firm() {
      if (this.room === '') {
        this.$modal({
          titie: '提示',
          content: '请填写房间号'
        })
      }
      if (this.room !== '' && this.name === '') {
        this.$modal({
          title: '提示',
          content: '请填写昵称'
        })
      }
      if (this.room !== '' && this.name !== '') {
        fetch(ADDRESS.http + `/searchRoom/search`, {
          method: 'post',
          headers: {
            'Content-type': 'application/json'
          },
          body: JSON.stringify({
            roomId: this.room,
            userName: this.name
          })
        })
          .then(r => r.text())
          .then(d => {
            d = JSON.parse(d)
            if (d.status === 200) {
              this.$store.dispatch(SET_USERNAME, {
                username: this.name
              })
              this.$store.dispatch(SET_ROOMID, {
                roomid: this.room
              })
              this.$router.push({
                path: '/home'
              })
              this.$toast.success('加入房间成功！')
            } else if (d.status === 403) {
              this.$modal({
                titie: '错误',
                content: '加入房间失败！房间内有同名用户，请更改您的用户名。'
              })
              // this.$alert(
              //   "加入房间失败！房间内有同名用户，请更改您的用户名。",
              //   "错误",
              //   {
              //     confirmButtonText: "确定",
              //     type: "error",
              //     center: true,
              //   }
              // );
            } else if (d.status === 400) {
              this.$modal({
                titie: '错误',
                content: '加入房间失败！房间号不存在。'
              })
              // this.$alert("加入房间失败！房间号不存在。", "错误", {
              //   confirmButtonText: "确定",
              //   type: "error",
              //   center: true,
              // });
            } else {
              this.$toast.error('加入房间失败！')
            }
          })
          .catch(e => console.error(e))
      }
    }
  }
}
</script>

<style scoped>
.joinHome {
  max-width: 40rem;
  margin: auto;
  min-height: 100vh;
  text-align: center;
}
.joinHome_box {
  width: 45rem;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.hello {
  margin-bottom: 4rem;
  font-size: 2.5rem;
}
.joinHome_form1 {
  margin-bottom: 2rem;
  width: 100%;
}
.joinHome_box h3 {
  display: inline-block;
  font-weight: 550;
  margin-right: 1rem;
  font-size: 1.4rem;
}
.el-input {
  max-width: 20rem;
}
.joinHome_btn {
  text-align: center;
  margin-top: 4rem;
}
.one {
  margin-right: 1rem;
}
</style>
